package persistence;

import java.io.Serializable;


import model.Game;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class DaoGameHibernate implements DaoGame {
	
	private static DaoGame dao;
	
	private SessionFactory factory;
	
	private DaoGameHibernate(String pathModel){
		factory = getConfiguration(pathModel).buildSessionFactory(); 
	}

	public static Configuration getConfiguration(String pathModel) {
		return new Configuration().configure(pathModel);
	}
	
	public static DaoGame instance(String pathModel){
		dao = new DaoGameHibernate(pathModel);
		return dao;
	}
	
	public void save(Game obj){
		Session session = factory.openSession();
		try{
			session.beginTransaction();
			session.saveOrUpdate(obj);
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{ session.close(); }
	}
	
	public Game find(Long id){
		Session session = factory.getCurrentSession();
		return (Game) session.get(Game.class, id);
	}
	
	
	public void touch(Game game) {
		// TODO Auto-generated method stub
		
	}

	public void update(Game obj) {
		// TODO Auto-generated method stub
		
	}

}
